import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import UnoCSS from 'unocss/vite'
import { wrapperEnv } from './vite/util'
import alias from './vite/alias'
import { createProxy } from './vite/proxy'
import setupPlugins from './vite/plugins/index'

export default ({ command, mode }) => {
  const isBuild = command === 'build'
  const root = process.cwd()
  const env = loadEnv(mode, root)

  const viteEnv = wrapperEnv(env)
  const { VITE_PORT, VITE_PROXY } = viteEnv

  return defineConfig({
    base: loadEnv(mode, process.cwd()).VITE_PUBLIC_PATH,
    plugins: [...setupPlugins(), vue(), UnoCSS()],
    resolve: {
      alias,
    },
    server: {
      host: true,
      port: VITE_PORT,
      open: true,
      proxy: createProxy(VITE_PROXY),
    },
  })
}
